<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>WISSC轮滑教培服务网-查询系统</title>
    <#assign base=request.contextPath  />
    <link rel="icon" href="${base}/favicon.ico">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link rel="stylesheet" href="${base}/frame/layui/css/layui.css?t=1534970980648" media="all">
    <script type="text/javascript" src="${base}/js/feng.js"></script>
    <script src="${base}/frame/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${base}/js/ztree/jquery-1.4.4.min.js"></script>
    <script src="${base}/js/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="${base}/css/hzw-city-picker.css">
    <script type="text/javascript" src="${base}/js/city-data.js"></script>
    <script type="text/javascript" src="${base}/js/hzw-city-picker.min.js"></script>
    <script type="text/javascript" src="${base}/js/city-zk.js"></script>
    <script type="text/javascript" src="${base}/js/province.js"></script>
    <style type="text/css">
        form {
            padding: 10px;
        }

    </style>
    <script>
        var defaults = {
            s1: 'provid',
            s2: 'cityid',
            s3: 'areaid',
            v1: null,
            v2: null,
            v3: null
        };
    </script>
</head>
<body>
<form class="layui-form" id="myform" style="margin: 10px">

    <div class="layui-form-item">
        <label class="layui-form-label"><a style="color:red">*</a>&nbsp;单位名称</label>
        <div class="layui-input-inline">
            <input name="unitName" id="unitName" lay-verify="required" autocomplete="off" class="layui-input isNull"
                   type="text">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><a style="color:red">*</a>&nbsp;开始时间</label>
            <div class="layui-input-inline">
                <input name="startTime" id="startTime" lay-verify="required" autocomplete="off"
                       class="layui-input isNull" type="text">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label"><a style="color:red">*</a>&nbsp;结束时间</label>
            <div class="layui-input-inline">
                <input name="endTime" id="endTime" lay-verify="required" autocomplete="off" class="layui-input isNull"
                       type="text">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><a style="color:red">*</a>&nbsp;选择地区</label>
            <div class="layui-input-inline">
                <select name="provid" id="provid" lay-filter="provid" lay-verify="required" class="isNull">
                    <option value="">请选择国家</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <select name="cityid" id="cityid" lay-filter="cityid" lay-verify="required">
                    <option value="">请选择省</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <select name="areaid" id="areaid" lay-filter="areaid" lay-verify="required">
                    <option value="">请选择市</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><a style="color:red">*</a>&nbsp;授权类型</label>
        <div class="layui-input-inline">
            <select name="grantType" lay-verify="required" id="grantType" class=" isNull">
                <option value="">请选择授权类型</option>
                <option value="1">授权实施会员单位</option>
                <option value="2">授权考核基地</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><a style="color:red">*</a>&nbsp;授权等级</label>
        <div class="layui-input-inline">
            <select name="grantLevel" lay-verify="required" id="grantLevel" class=" isNull">
                <option value="">请选资质等级</option>
                <option value="1">1-10级实施单位</option>
                <option value="2">1-7级实施单位</option>
                <option value="3">1-4级实施单位</option>
                <option value="4">A场地</option>
                <option value="5">B场地</option>
                <option value="6">C场地</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label"><a style="color:red">*</a>&nbsp;机构简介</label>
        <div class="layui-input-block">
            <textarea name="brief" id="brief" placeholder="请输入内容" class="layui-textarea isNull"></textarea>
            <span><span id="textNum">0</span>/200</span>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test2" style="background-color: #0092EF;">证书(最多五张图片)</button>
            <blockquote class="layui-elem-quote layui-quote-nm" id="aaaa" style="margin-top: 10px;">
                预览图：
                <div class="layui-upload-list " id="demo2"></div>
            </blockquote>
        </div>

        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test3" style="background-color: #0092EF;">标志图片(要求单图片)</button>
            <div class="layui-upload-list" style="border: 1px solid #e6e6e6;height: 100px;width: 180px">
                <img style="height: 100%;width: 100%" class="layui-upload-img" id="demo3">
                <p id="demoText"></p>
            </div>
        </div>
    </div>

    </div>
    <div class="layui-form-item">
        <div class="layui-input-inline">
            <button class="layui-btn " lay-submit lay-filter="formDemo" type="button"
                    onclick="onSubmit()" style="background-color: #0092EF;">立即提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script>
    function resetText() {
        document.getElementById("myform").reset();
    }
</script>
<script>
    var fileIds = [];
    layui.use('upload', function () {
        var $ = layui.jquery
            , upload = layui.upload;

        //多图片上传
        upload.render({
            elem: '#test2'
            , url: '/authorization/upload'
            , multiple: true
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo2').append('<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" style="width:100px;height:100px">')
                });
            }
            , done: function (res) {
                layer.msg(res.msg)
                fileIds.push(res.data.fileId)
            }
        });
    })
</script>
<script>
    var a;
    layui.use('upload', function () {
        var $ = layui.jquery
            , upload = layui.upload;
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test3'
            , url: '${base}/file/uploadFile'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo3').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //上传成功
                layer.msg("上传成功")
                a += res.data;
                return res.data;

            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    })


</script>
<script>
    $('#brief').on("keyup", function () {
        $('#textNum').text($('#brief').val().length);//这句是在键盘按下时，实时的显示字数
        if ($('#brief').val().length > 100) {
            $('#textNum').text(200);//长度大于100时0处显示的也只是100
            $('#brief').val($('#brief').val().substring(0, 200));//长度大于100时截取钱100个字符
        }
    })
    $('#textNum').text($('#brief').val().length);//这句是在刷新的时候仍然显示字数
    var cityPicker = new HzwCityPicker({
        data: data,
        target: 'cityChoice',
        valType: 'k',
        hideCityInput: {
            name: 'city',
            id: 'city'
        },
        hideProvinceInput: {
            name: 'areaId',
            id: 'areaId',
            val: 'q'
        },
        callback: function () {
        }
    });

    cityPicker.init();

    layui.use(['form', 'layedit', 'laydate', 'layer'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;


        //执行一个laydate实例
        laydate.render({
            elem: '#startTime',
            format: 'yyyy-MM-dd',
            done: function (value, date, endDate) {
                var startDate = new Date(value).getTime();
                var endTime = new Date($('#endTime').val()).getTime();
                if (endTime < startDate) {
                    layer.msg('结束时间不能小于开始时间');
                    $('#startTime').val($('#endTime').val());
                }
            }
        })
        laydate.render({ //结束时间
            elem: '#endTime',
            format: 'yyyy-MM-dd',
            done: function (value, date, endDate) {
                var startDate = new Date($('#startTime').val()).getTime();
                var endTime = new Date(value).getTime();
                if (endTime < startDate) {
                    layer.msg('结束时间不能小于开始时间');
                    $('#endTime').val($('#startTime').val());
                }
            }
        })
    });
    var pIds;
    var a = "";

    //添加
    function onSubmit() {
        if (!isNull()) {
            return;
        }

        if (fileIds == null || fileIds == "" || fileIds == undefined) {
            layer.msg('<span style="color:red; ">请上传多图片</span>')
        } else if (a == null || a == "") {
            layer.msg('<span style="color:red; ">请上传单图片</span>')
        } else {


            var jsonData = {};
            var provid = $("#provid").val()
            var cityid = $("#cityid").val();
            var areaid = $("#areaid").val();
            var str = areaid.replace(/^\"|\"$/g, '');
            var sum = provid + "/" + cityid + "/" + str + "/";


            jsonData.unitName = $("#unitName").val();
            jsonData.startTime = (new Date($("#startTime").val())).getTime();
            jsonData.endTime = (new Date($("#endTime").val())).getTime();
            jsonData.city = sum;
            jsonData.grantType = $("#grantType").val();
            jsonData.grantLevel = $("#grantLevel").val();
            jsonData.brief = $("#brief").val();
            jsonData.logoUrl = a;
            $.ajax({
                type: "POST", //提交方式
                traditional: true,//这里设置为true,不然后台接收到的参数会带上[]，导致数据无法正常接收
                url: "${base}/authorization/authorizationInsert",//路径
                data: {pIds: fileIds, param: JSON.stringify(jsonData)},
                success: function (data) {//返回数据根据结果进行相应的处理
                    data = JSON.parse(data)
                    if (data.flag === true) {
                        layer.msg("添加成功,请到授权机构查询模块查看");
                         setTimeout(function () {
                            window.location.href = "${base}/authorization/authorization_add.html";
                        }, 1500)
                    } else {
                        layer.msg(data.msg)
                    }
                }
            });
        }
    }
</script>
</body>
</html>
